package com.company.oa.announcement.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.announcement.dto.TemplateDTO;
import com.company.oa.announcement.entity.AnnouncementTemplate;
import com.company.oa.announcement.service.AnnouncementTemplateService;
import com.company.oa.common.api.Result;
import com.company.oa.security.annotation.RequiresPermissions;
import com.company.oa.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/announcement-templates")
public class AnnouncementTemplateController {

    @Autowired
    private AnnouncementTemplateService templateService;

    /**
     * 分页查询模板列表
     */
    @GetMapping("/page")
    @RequiresPermissions("announcement:template:list")
    public Result<IPage<AnnouncementTemplate>> getTemplatePage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            AnnouncementTemplate template) {
        IPage<AnnouncementTemplate> pageResult = templateService.getTemplatePage(page, size, template);
        return Result.success(pageResult);
    }

    /**
     * 创建模板
     */
    @PostMapping
    @RequiresPermissions("announcement:template:create")
    public Result<Boolean> createTemplate(@RequestBody @Valid TemplateDTO templateDTO) {
        Long userId = SecurityUtils.getUserId();
        String userName = SecurityUtils.getUsername();

        boolean result = templateService.createTemplate(templateDTO, userId, userName);
        return Result.success(result);
    }

    /**
     * 更新模板
     */
    @PutMapping
    @RequiresPermissions("announcement:template:update")
    public Result<Boolean> updateTemplate(@RequestBody @Valid TemplateDTO templateDTO) {
        boolean result = templateService.updateTemplate(templateDTO);
        return Result.success(result);
    }

    /**
     * 删除模板
     */
    @DeleteMapping("/{id}")
    @RequiresPermissions("announcement:template:delete")
    public Result<Boolean> deleteTemplate(@PathVariable Long id) {
        boolean result = templateService.deleteTemplate(id);
        return Result.success(result);
    }
}